﻿using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

namespace CNKI.TPI.Web.Base
{
    public class StringHelper
    {
        public static string RemoveHighlightFlag(string content)
        {
            string result = content;
            if (content.Contains("$$$") && content.Contains("###"))
            {
                result = content.Replace("$$$", "").Replace("###", "");
            }
            return result;
        }
        public static string ConvertHighlightFlag(string content)
        {
            string result = content;
            if (content.Contains("$$$") && content.Contains("###"))
            {
                result = result.Replace("###", "<span style=\"color:red;margin: 0px\">");
                result = result.Replace("$$$", "</span>");
            }
            return result;

        }

        public static string RemoveHighlightTag(string content)
        {
            string result = content;
            if (content.Contains("<span style=\"color:red;margin: 0px\">") && content.Contains("</span>"))
            {
                result = result.Replace("<span style=\"color:red;margin: 0px\">","");
                result = result.Replace("</span>","");
            }
            return result;

        }

        public static string CutString(string content, int length)
        {
            string result = null;
            if (content.Length > length)
            {
                result = content.Substring(0, length) + "...";
            }
            else
            {
                result = content;
            }
            return result;
        }

        public static string RemoveHTML(string Htmlstring)
        {
            if (Htmlstring == null)
            {
                return "";
            }
            //删除脚本  
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML  
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"\\s+", "", RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }

        public static string GetMD5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String;
        }

        public static string GetLowerKey(string Htmlstring, string split = "")
        {
            string strR = Regex.Replace(Htmlstring, @"[^\dA-Za-z\u4e00-\u9fa5]", split).ToLower();
            return strR.Replace(split + split, split);
        }
        /// <summary>
        /// 去除HTML标记
        /// </summary>
        /// <param name="Htmlstring">包括HTML的源码 </param>
        /// <returns>已经去除后的文字</returns>
        public static string NoHTML(string Htmlstring)
        {
            MatchCollection mcImg = Regex.Matches(Htmlstring, @"<(\bimg\b)[^>]*>");
            List<string> imgList = new List<string>();
            for (int i = 0; i < mcImg.Count; i++)
            {
                imgList.Add(mcImg[i].Value);
            }
            Htmlstring = Regex.Replace(Htmlstring, @"<(\bimg\b)[^>]*>", "imgHtmlString", RegexOptions.IgnoreCase);

            MatchCollection mctable = Regex.Matches(Htmlstring, @"<table[\s\S]*?>[\s\S]*?<\/table>");
            List<string> tableList = new List<string>();
            for (int i = 0; i < mctable.Count; i++)
            {
                tableList.Add(mctable[i].Value);
            }
            Htmlstring = Regex.Replace(Htmlstring, @"<table[\s\S]*?>[\s\S]*?<\/table>", "tableHtmlString", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"<title>[\s\S]*</title>|<[^>]*>", "", RegexOptions.IgnoreCase);
            //Htmlstring = Regex.Replace(Htmlstring, @"<(?!\bimg\b)[^>]*>", "", RegexOptions.IgnoreCase);
            //换行替换br
            Htmlstring = Regex.Replace(Htmlstring, @"[\r\n]+", "<br/>", RegexOptions.IgnoreCase);
            //将img标签内的br 去掉
            //Htmlstring = Regex.Replace(Htmlstring, @"\b<*<br/>*>\b", " ", RegexOptions.IgnoreCase);

            //Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            //Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);



            Regex r = new Regex("tableHtmlString");
            for (int i = 0; i < tableList.Count; i++)
            {
                Htmlstring = r.Replace(Htmlstring, tableList[i], 1, 0);
            }
            Regex r1 = new Regex("imgHtmlString");
            for (int i = 0; i < imgList.Count; i++)
            {
                Htmlstring = r1.Replace(Htmlstring, imgList[i], 1, 0);
            }
            //双引号换成单引号
            Htmlstring = Regex.Replace(Htmlstring, "\"", "\'", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"src='", "src='/html/", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"<br/>src", "src", RegexOptions.IgnoreCase);

            return Htmlstring;
        }

        /// <summary>
        /// 将xml 通过xslt转换后 以字符串形式输出
        /// </summary>
        /// <param name="xml">XmlDocument对象</param>
        /// <param name="filePath">xslt文件路径</param>
        /// <returns>经过转换后的字符串</returns>
        public static string XmlToString(XmlDocument xml, string filePath)
        {
            StringBuilder sb = new StringBuilder();//需要输出的字符串类
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;//编写xml声明
                settings.ConformanceLevel = ConformanceLevel.Fragment;//设置xml文件符合的一致性级别
                settings.CloseOutput = false;//settings关闭时不关闭基础流

                TextWriter tw = new StringWriter(sb);
                XmlWriter xw = XmlWriter.Create(tw, settings);
                XslCompiledTransform xst = new XslCompiledTransform();
                //加载格式化文件
                string str = HttpContext.Current.Request.ApplicationPath + "/" + filePath;
                xst.Load(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + filePath));
                xst.Transform(xml, null, xw);//把转换的结果输出到sb中
            }
            catch { return ""; }
            return sb.ToString();
        }
        /// <summary>
        /// 将string转换成int
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static List<int> GetListInt(string str, char c)
        {
            List<int> lsInt = new List<int>();
            if (str == "" || str == null)
            {
                return lsInt;
            }

            var strList = str.Split(c);
            foreach (var item in strList)
            {
                if (item != "")
                {
                    try
                    {
                        lsInt.Add(int.Parse(item));
                    }
                    catch { }
                }
            }
            return lsInt;
        }
        /// <summary>
        /// 得到List<int>
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static List<int> GetListInt(object obj, char c)
        {
            if (obj == null || string.IsNullOrEmpty(obj.ToString()))
            {
                return null;
            }
            string str = obj.ToString();
            List<int> lsInt = new List<int>();
            var strList = str.Split(c);
            foreach (var item in strList)
            {
                if (item != "")
                {
                    try
                    {
                        lsInt.Add(int.Parse(item));
                    }
                    catch { }
                }
            }
            return lsInt;
        }
        /// <summary>
        /// 得到List<int>
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static List<string> GetListString(IEnumerable<int?> obj)
        {
            if (obj == null || string.IsNullOrEmpty(obj.ToString()))
            {
                return null;
            }
            List<string> list = new List<string>();
            foreach (var item in obj)
            {
                if (item != null)
                {
                    try
                    {
                        list.Add(item.ToString());
                    }
                    catch { }
                }
            }
            return list;
        }
        /// <summary>
        /// 切词
        /// </summary>
        /// <param name="str"></param>
        /// <param name="count"></param>
        /// <param name="strSL"></param>
        /// <returns></returns>
        public static string SubString(object obj, int count, string strSL)
        {
            if (obj != null && obj.ToString() != "")
            {
                if (obj.ToString().Length > count)
                {
                    return obj.ToString().Remove(count) + strSL;
                }
                else
                {
                    return obj.ToString();
                }
            }
            else
            {
                return "";
            }
        }

        public static string GetStr(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        public static string GetStringByDate(DateTime? date)
        {
            if (date != null)
            {
                return date.Value.ToString("yyyy-MM-dd");
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 将List 转成String
        /// </summary>
        /// <returns></returns>
        public static string ConvertListToStr(IEnumerable<string> list)
        {
            if (list == null || list.Count() == 0)
            {
                return "";
            }
            string BacStr = "";
            foreach (var item in list)
            {
                BacStr += item + ",";
            }
            if (BacStr.Trim() == "无")
            { return ""; }
            if (BacStr.Trim() == "无,")
            { return ""; }
            if (BacStr.Trim() == "无;")
            { return ""; }
            if (BacStr.Trim() == "无，")
            { return ""; }
            return BacStr.TrimEnd(',');
        }
        /// <summary>
        /// 将List 转成String
        /// </summary>
        /// <returns></returns>
        public static string ConvertListToStr(List<string> list)
        {
            if (list == null || list.Count() == 0)
            {
                return "";
            }
            string BacStr = "";
            foreach (var item in list)
            {
                BacStr += item;
            }
            return BacStr;
        }


        public static List<string> ConvertStrsToList(string[] strs)
        {
            if (strs == null || strs.Count() == 0)
            {
                return null;
            }
            List<string> ls = new List<string>();
            foreach (var item in strs)
            {
                ls.Add(item);
            }
            return ls;

        }

        public static string GetIP()
        {
            string ipAddress = HttpContext.Current.Request.UserHostAddress;
            if (ipAddress.ToLower().Equals("::1"))
            {
                ipAddress = "127.0.0.1";
            }
            return ipAddress;
        }
        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string CountStringSize(string Size)
        {
            if (Size == null || Size == "")
            {
                return "";
            }
            string m_strSize = "";
            long FactSize = 0;
            try
            {
                FactSize = Convert.ToInt64(Size);
            }
            catch
            {
                return "";
            }
            if (FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }
        public static string CountSize(double? Size)
        {
            if (Size == null)
            {
                return "";
            }
            string m_strSize = "";
            double FactSize = 0;
            FactSize = Size.Value;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }
        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string CountSize(long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }


        /// <summary>
        /// 整型补齐2位 前面加0  如 1转换成01
        /// </summary>
        /// <returns></returns>
        public static string GetIntString(int a)
        {
            if (a.ToString().Length == 1)
            {
                return "0" + a.ToString();
            }
            else return a.ToString();
        }

        public static string GetFunID(string fid)
        {//01.01.
            fid = fid.TrimEnd('.');
            var flist = fid.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            int flast = int.Parse(flist[flist.Length - 1]);
            string fBefore = "";
            for (int i = 0; i < flist.Length - 1; i++)
            {
                fBefore += flist[i] + ".";
            }

            if (flast.ToString().Length == 1)
            {
                return fBefore + "0" + (flast + 1).ToString() + ".";
            }
            else
                return fBefore + (flast + 1).ToString() + ".";
        }

        /// <summary>
        /// 对字符串进行 HTML 编码操作
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string strEncode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "&apos;");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }


        /// <summary>
        /// 对 HTML 字符串进行解码操作
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string strDecode(string str)
        {
            str = str.Replace("<br>", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }


        /// <summary>
        /// 根据正则规则得到中间部分
        /// </summary>
        /// <param name="stext">文本</param>
        /// <param name="regular">规则</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetMidLine(string stext, string regular, bool isBegin = true)
        {

            Dictionary<string, string> dic = new Dictionary<string, string>(); //存放分割后字符
            Regex r = new Regex(regular, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            MatchCollection mc = r.Matches(stext);
            if (mc.Count >= 1)
            {
                for (int i = 0; i < mc.Count - 1; i++)
                {
                    string sKey = mc[i].Value;
                    string sValue = stext.Substring(mc[i].Index, mc[i + 1].Index - mc[i].Index);
                    if (isBegin == false)
                    {
                        sValue = GetBegTrimStr(sValue, sKey);
                    }
                    if (!dic.Keys.Contains(sKey))
                    {
                        dic.Add(sKey, sValue);
                    }
                }
                //dic.Add(mc[mc.Count - 1].Value, stext.Substring(mc[mc.Count - 1].Index, stext.Length - mc[mc.Count - 1].Index));
                if (!dic.Keys.Contains(mc[mc.Count - 1].Value))
                {
                    if (isBegin == false)
                    {
                        dic.Add(mc[mc.Count - 1].Value, GetBegTrimStr(stext.Substring(mc[mc.Count - 1].Index), mc[mc.Count - 1].Value));
                    }
                    else
                    {
                        dic.Add(mc[mc.Count - 1].Value, stext.Substring(mc[mc.Count - 1].Index));
                    }
                }
            }
            return dic;
        }
        /// <summary>
        /// 移除前面字符
        /// </summary>
        /// <param name="stext"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetBegTrimStr(string stext, string str)
        {
            stext = stext.Trim();
            while (stext.IndexOf(str) == 0)
            {
                stext = stext.Substring(str.Length);
            }
            return stext;
        }


        /// <summary>
        /// 根据规则得到中间部分
        /// </summary>
        /// <param name="stext">文本</param>
        /// <param name="regular1">开始部分</param>
        /// <param name="regular2">结束规则</param>
        /// <param name="svaule">匹配值</param>
        /// <returns></returns>
        public static string GetMidString(string stext, string regular1, string regular2, out string svaule)
        {
            Regex r1 = new Regex(regular1, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
            Match m1 = r1.Match(stext);
            string sReturn = string.Empty;
            if (m1.Success)
            {
                svaule = m1.Value;
                stext = stext.Substring(m1.Index + m1.Value.Length);
                Regex r2 = new Regex(regular2, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
                Match m2 = r2.Match(stext);

                if (m2.Success)
                {
                    sReturn = stext.Substring(0, m2.Index);
                }
                else
                {
                    sReturn = stext.Substring(0);
                }
            }
            else
            {
                svaule = string.Empty;
            }
            return sReturn;
        }


        public static string GetTrimStr(string stext, string str)
        {
            stext = stext.Trim();
            while (stext.IndexOf(str) == 0)
            {
                stext = stext.Substring(str.Length);
            }
            while (stext.LastIndexOf(str) > 0 && stext.LastIndexOf(str) == (stext.Length - str.Length))
            {
                stext = stext.Remove(stext.LastIndexOf(str)).Trim();
            }
            return stext;
        }


        /// <summary>
        /// 根据正则规则得到中间部分
        /// </summary>
        /// <param name="stext">文本</param>
        /// <param name="regular1">开始规则</param>
        /// <param name="regular2">结束规则</param>
        /// <returns></returns>
        public static string GetMidLine(string stext, string regular1, string regular2)
        {
            Regex r1 = new Regex(regular1, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
            Match m1 = r1.Match(stext);
            string sReturn = string.Empty;
            if (m1.Success)
            {
                stext = stext.Substring(m1.Index + m1.Value.Length);
                Regex r2 = new Regex(regular2, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
                Match m2 = r2.Match(stext);

                if (m2.Success)
                {
                    sReturn = stext.Substring(0, m2.Index);
                }
                else
                {
                    sReturn = stext.Substring(0);
                }
            }
            return sReturn;
        }

        public static List<string> GetMidRegular(string stext, string regular)
        {
            List<string> list = new List<string>(); //存放分割后字符
            Regex r = new Regex(regular, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            MatchCollection mc = r.Matches(stext);
            if (mc.Count > 1)
            {
                foreach (Match m in mc)
                {
                    list.Add(m.Value);
                }
            }
            return list;
        }

        /// <summary>
        /// 得到字典中value
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string GetDicValueByKey(Dictionary<string, string> dic, string sKey)
        {
            if (dic.Keys.Contains(sKey))
            {
                return dic[sKey];
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 移除结尾字符
        /// </summary>
        /// <returns></returns>
        public static string GetEndTrimStr(string stext, string str)
        {
            stext = stext.Trim();
            while (stext.LastIndexOf(str) > 0 && stext.LastIndexOf(str) == (stext.Length - str.Length))
            {
                stext = stext.Remove(stext.LastIndexOf(str)).Trim();
            }
            return stext;
        }


        /// <summary>
        /// 字符串相似度函数
        /// </summary>
        /// <param name="strOriginal">两个字符串比较时的模板</param>
        /// <param name="strCompare">以模板为准要参与比较的字符串</param>
        /// <returns>int类型的两个字符的相似度(除以100则为百分比)</returns>
        public static int StringCompare(string strOriginal, string strCompare)
        {
            int len1 = strOriginal.Length;
            int len2 = strCompare.Length;
            char[] cStr1 = strOriginal.ToCharArray();
            char[] cStr2 = strCompare.ToCharArray();
            int[,] dif = new int[len1 + 1, len2 + 1];

            for (int a = 0; a <= len1; a++)
            {
                dif[a, 0] = a;
            }
            for (int a = 0; a <= len2; a++)
            {
                dif[0, a] = a;
            }

            int temp;
            for (int i = 1; i <= len1; i++)
            {
                for (int j = 1; j <= len2; j++)
                {
                    if (cStr1[i - 1] == cStr2[j - 1])
                    {
                        temp = 0;
                    }
                    else
                    {
                        temp = 1;
                    }
                    dif[i, j] = Math.Min(Math.Min(dif[i - 1, j - 1] + temp, dif[i, j - 1] + 1), dif[i - 1, j] + 1);
                }
            }

            int similarity = (Convert.ToInt32(1 - (float)dif[len1, len2] / Math.Max(strOriginal.Length, strCompare.Length))) * 100;
            return similarity;
        }

        /// <summary>
        /// 根据正则规则得到需要的部分
        /// </summary>
        /// <param name="stext">文本</param>
        /// <param name="regular">规则</param>
        /// <returns></returns>
        public static List<string> GetMidLine(string stext, string regular)
        {
            List<string> dic = new List<string>(); //存放分割后字符
            Regex r1 = new Regex(regular);
            MatchCollection m1 = r1.Matches(stext);
            Match m2 = r1.Match(stext);
            foreach (var item in m1)
            {
                dic.Add(item.ToString());
            }
            return dic;
        }


        /// <summary>
        /// 对传递的参数字符串进行处理，防止注入式攻击
        /// </summary>
        /// <param name="str">传递的参数字符串</param>
        /// <returns>String</returns>
        public static string ConvertStr(string str)
        {
            str = str.Trim();
            str = str.Replace("'", "''");
            str = str.Replace(";--", "");
            str = str.Replace("=", "");
            str = str.Replace(" or ", "");
            str = str.Replace(" and ", "");

            //过滤SQL的关键字符串
            //strFilteringString = strFilteringString.Replace("-", "－");
            //strFilteringString = strFilteringString.Replace(";", "；");
            //strFilteringString = strFilteringString.Replace(",", "，");
            //strFilteringString = strFilteringString.Replace("/", "、");
            //strFilteringString = strFilteringString.Replace("(", "（");
            //strFilteringString = strFilteringString.Replace(")", "）");
            //strFilteringString = strFilteringString.Replace("[", "【");
            //strFilteringString = strFilteringString.Replace("]", "】");
            //strFilteringString = strFilteringString.Replace("{", "｛");
            //strFilteringString = strFilteringString.Replace("}", "｝");
            //strFilteringString = strFilteringString.Replace("%", "％");
            //strFilteringString = strFilteringString.Replace("@", "＠");
            //strFilteringString = strFilteringString.Replace("*", "×");
            //strFilteringString = strFilteringString.Replace("!", "！");
            //strFilteringString = strFilteringString.Replace("'", "’");
            //strFilteringString = strFilteringString.Replace("\"", "“");
            //strFilteringString = strFilteringString.Replace("?", "？");
            //strFilteringString = strFilteringString.Replace("\\", "＼");
            return str;
        }

        public static string removeStr(string src, string target)
        {
            string TempFileName = "";
            //更新主表SYS_FLD_DIGITFILENAME字段信息
            if (!String.IsNullOrEmpty(src))
            {
                if (src.IndexOf("|") >= 0)
                {
                    string[] ArrFile = src.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < ArrFile.Length; i++)
                    {
                        if (ArrFile[i].ToString().ToLower() != target.ToLower())
                        {
                            TempFileName += ArrFile[i].ToString() + "|";
                        }
                    }
                    src = TempFileName.Substring(0, TempFileName.LastIndexOf("|"));
                }
                else
                {
                    if (src.ToLower() == target.ToLower())
                    {
                        src = "";
                    }
                }
            }
            else
            {
                src = "";
            }
            return src;
        }

        public static string GetSubmitFieldTypeDesc(SubmitFieldType rightType)
        {
            string result = null;
            switch (rightType)
            {
                case SubmitFieldType.TextBox:
                    result = "文本框";
                    break;
                case SubmitFieldType.ComboBox:
                    result = "下拉列表";
                    break;
                case SubmitFieldType.EmailBox:
                    result = "邮箱";
                    break;
                case SubmitFieldType.FullTestBox:
                    result = "全文框";
                    break;
                case SubmitFieldType.KeyBox:
                    result = "关键词";
                    break;
                case SubmitFieldType.MultiCheckBox:
                    result = "复选框";
                    break;
                case SubmitFieldType.NaviBox:
                    result = "导航";
                    break;
                case SubmitFieldType.SingleCheckBox:
                    result = "单选按钮";
                    break;
                case SubmitFieldType.TimeBox:
                    result = "时间框";
                    break;
                //case SubmitFieldType.ReferenceBox:
                //    result = "参考文献";
                //    break;
                default:
                    break;
            }
            return result;
        }
        /// <summary>
        /// 过滤空格和换行
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable DealInfo(DataTable dt)
        {
            if (!ConfigHelper.IsSpaceEscape())
            {
                return dt;
            }
            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataColumn fc in dt.Columns)
                {
                    dr[fc.ColumnName] = DealInfo(dr[fc.ColumnName].ToString());
                }
            }
            return dt;
        }
        /// <summary>
        /// 过滤空格与换行
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string DealInfo(string item)
        {
            if (!ConfigHelper.IsSpaceEscape())
            {
                return item;
            }
            if (string.IsNullOrEmpty(item))
            {
                return "";
            }
            item = item.Replace(" ", "&nbsp;").Replace(Convert.ToChar(13).ToString(), "<br>");
            return item;
        }

        public static string UnDealInfo(string item)
        {
            if (!ConfigHelper.IsSpaceEscape())
            {
                return item;
            }
            if (string.IsNullOrEmpty(item))
            {
                return "";
            }
            item = item.Replace("&nbsp;", " ").Replace("<br>", Convert.ToChar(13).ToString());
            return item;
        }

        /// <summary>
        /// 对字符串进行 xml 编码操作
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string strXmlEncode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "&apos;");
            str = str.Replace("\"", "&quot;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            return str;
        }
    }
}
